<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>Linux 修改用户属性 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>Linux 修改用户属性</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux/">Linux</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;us<br>ermod 命令修改系统帐户文件来反映通过命令行指定的变化    </p>
<h2 id="选项"><a href="#选项" class="headerlink" title="选项"></a>选项</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-a|–append&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##把用户追加到某些组中，仅与-G选项一起使用 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-c|–comment&#160;&#160;&#160;&#160;&#160;&#160;&#160;##修改/etc/passwd文件第五段comment</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-d|–home&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##修改用户的家目录通常和-m选项一起使用 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-e|–expiredate&#160;&#160;&#160;&#160;&#160;&#160;##指定用户帐号禁用的日期，格式YY-MM-DD </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-f|–inactive&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ##用户密码过期多少天后采用就禁用该帐号，0表示密码已过期就禁用帐号，-1表示禁用此功能，默认值是-1 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-g|–gid&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; ##修改用户的gid，改组一定存在</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-G|–groups&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##把用户追加到某些组中，仅与-a选项一起使用 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-l|–login&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##修改用户的登录名称</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-L|–lock&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##锁定用户的密码 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-m|–move-home&#160;&#160;&#160;##修改用户的家目录通常和-d选项一起使用 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-s|–shell&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##修改用户的shell </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-u|–uid&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##修改用户的uid，该uid必须唯一 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-U|–unlock&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;##解锁用户的密码 </p>
<h2 id="示例"><a href="#示例" class="headerlink" title="示例"></a>示例</h2><h3 id="1-新建用户-test-，密码-test-，另外添加-usertest-组"><a href="#1-新建用户-test-，密码-test-，另外添加-usertest-组" class="headerlink" title="1.新建用户 test ，密码 test ，另外添加 usertest 组"></a>1.新建用户 test ，密码 test ，另外添加 usertest 组</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#useradd test </span></div><div class="line"><span class="comment">#echo "test" | passwd --stdin test </span></div><div class="line"><span class="comment">#groupadd usertest</span></div></pre></td></tr></table></figure>
<h3 id="2-把-test-用户加入-usertest-组"><a href="#2-把-test-用户加入-usertest-组" class="headerlink" title="2.把 test 用户加入 usertest 组"></a>2.把 test 用户加入 usertest 组</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#usermod -aG usertest test ##多个组之间用空格隔开 </span></div><div class="line"><span class="comment">#id test </span></div><div class="line">uid=500(<span class="built_in">test</span>) gid=500(<span class="built_in">test</span>) groups=500(<span class="built_in">test</span>),501(usertest)</div></pre></td></tr></table></figure>
<h3 id="3-修改-test-用户的家目录"><a href="#3-修改-test-用户的家目录" class="headerlink" title="3.修改 test 用户的家目录"></a>3.修改 test 用户的家目录</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#usermod -md /home/usertest </span></div><div class="line"><span class="comment">#ls /home </span></div><div class="line">usertest</div></pre></td></tr></table></figure>
<h3 id="4-修改用户名"><a href="#4-修改用户名" class="headerlink" title="4.修改用户名"></a>4.修改用户名</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#usermod -l urchin(新用户名称)  test(原来用户名称) </span></div><div class="line"><span class="comment">#id urchin </span></div><div class="line">uid=500(urchin) gid=500(<span class="built_in">test</span>) groups=500(<span class="built_in">test</span>),501(usertest)</div></pre></td></tr></table></figure>
<h3 id="5-锁定-urchin-的密码"><a href="#5-锁定-urchin-的密码" class="headerlink" title="5.锁定 urchin 的密码"></a>5.锁定 urchin 的密码</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># sed -n '$p' /etc/shadow </span></div><div class="line">urchin:<span class="variable">$6</span><span class="variable">$1PwPVBn5</span><span class="variable">$o</span>.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t \</div><div class="line">Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::: </div><div class="line"><span class="comment">#usermod -L urchin </span></div><div class="line"><span class="comment"># sed -n '$p' /etc/shadow </span></div><div class="line">urchin:!<span class="variable">$6</span><span class="variable">$1PwPVBn5</span><span class="variable">$o</span>.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t \</div><div class="line">Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:::</div></pre></td></tr></table></figure>
<h3 id="6-解锁-urchin-的密码"><a href="#6-解锁-urchin-的密码" class="headerlink" title="6.解锁 urchin 的密码"></a>6.解锁 urchin 的密码</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#usermod -U urchin </span></div><div class="line"><span class="comment"># sed -n '$p' /etc/shadow </span></div><div class="line">urchin:<span class="variable">$6</span><span class="variable">$1PwPVBn5</span><span class="variable">$o</span>.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t \ </div><div class="line">Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:::</div></pre></td></tr></table></figure>
<h3 id="7-修改用户的-shell"><a href="#7-修改用户的-shell" class="headerlink" title="7.修改用户的 shell"></a>7.修改用户的 shell</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#sed '$!d' /etc/passwd </span></div><div class="line">urchin:x:500:500::/home/usertest:/bin/bash </div><div class="line"><span class="comment">#usermod -s /bin/sh urchin </span></div><div class="line"><span class="comment">#sed -n '$p' /etc/passwd </span></div><div class="line">urchin:x:500:500::/home/usertest:/bin/sh</div></pre></td></tr></table></figure>
<h3 id="8-修改用户的-UID"><a href="#8-修改用户的-UID" class="headerlink" title="8.修改用户的 UID"></a>8.修改用户的 UID</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#usermod -u 578 urchin (UID必须唯一) </span></div><div class="line"><span class="comment">#id urchin </span></div><div class="line">uid=578(urchin) gid=500(<span class="built_in">test</span>) groups=500(<span class="built_in">test</span>),501(usertest)</div></pre></td></tr></table></figure>
<h3 id="9-修改用户的-GID"><a href="#9-修改用户的-GID" class="headerlink" title="9.修改用户的 GID"></a>9.修改用户的 GID</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#groupadd -g 578 test1 </span></div><div class="line"><span class="comment">#usermod -g 578 urchin (578组一定要存在) </span></div><div class="line"><span class="comment">#id urchin </span></div><div class="line">uid=578(urchin) gid=578(test1) groups=578(test1),501(usertest)</div></pre></td></tr></table></figure>
<h3 id="10-指定帐号过期日期"><a href="#10-指定帐号过期日期" class="headerlink" title="10.指定帐号过期日期"></a>10.指定帐号过期日期</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># sed -n '$p' /etc/shadow </span></div><div class="line">urchin:<span class="variable">$6</span><span class="variable">$1PwPVBn5</span><span class="variable">$o</span>.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t \ </div><div class="line">Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::: </div><div class="line"><span class="comment"># usermod -e 2012-09-11 urchin </span></div><div class="line"><span class="comment"># sed -n '$p' /etc/shadow </span></div><div class="line">urchin:<span class="variable">$6</span><span class="variable">$1PwPVBn5</span><span class="variable">$o</span>.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t \ </div><div class="line">Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7::15594:</div></pre></td></tr></table></figure>
<h3 id="11-指定用户帐号密码过期多少天后，禁用该帐号"><a href="#11-指定用户帐号密码过期多少天后，禁用该帐号" class="headerlink" title="11.指定用户帐号密码过期多少天后，禁用该帐号"></a>11.指定用户帐号密码过期多少天后，禁用该帐号</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># usermod -f 0 urchin </span></div><div class="line"><span class="comment"># sed -n '$p' /etc/shadow </span></div><div class="line">urchin:<span class="variable">$6</span><span class="variable">$1PwPVBn5</span><span class="variable">$o</span>.MIEYONzURQPvn/YqSp69kt2CIASvXhOnjv/t \ </div><div class="line">Z5m4NN6bJyLjCG7S6vmji/PFDfbyITdm1WmtV45CfHV5vux/:15594:0:99999:7:0:15594:</div></pre></td></tr></table></figure>
<h2 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;usermod 不允许改变正在线上的使用者帐号名称。当 usermod 用来改变 userID ,必须确认这名 user 没在电脑上执行任何程序</p>
<p>/etc/passwd<br>user_name:x:uid:gid:commnet:home:shell        </p>
<p>/etc/shadow<br>username:passwd:lastchg:min:max:warn:inactive:expire:flag<br>–用户名<br>–密码<br>–从1970年1月1日起到上次修改密码所经过的天数<br>–密码再过几天可以被变更(0表示随时可以改变)<br>–密码再过几天必须被变更(99999表示永不过期)<br>–密码过期前几天提醒用户(默认为一周)<br>–密码过期几天后帐号被禁用<br>–从1970年1月1日算起，多少天后账号失效      </p>

	

	
		<span class="different-posts"><a href="/2017/10/12/1. Linux 基础/11. Linux usermod 修改用户属性/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
